/************************************************************************/
/* Game                                                                 */
/************************************************************************/
#include "Game/Screen.h"

namespace TD_Game {

	const Coord2D Screen::m_statusBarPos(0, 0);
	const Coord2D Screen::m_rightMenuPos(768 + 16, 0);
	const Coord2D Screen::m_drawingCanvasPos(0, 30);
	const Coord2D Screen::m_infoBarPos(0, 576 + 30 + 16);
	const Coord2D Screen::m_canvasPos(0.054*screenWidth, 0.0869025*screenHeight);
	const Coord2D Screen::m_canvasBounds(0.751638*screenWidth, 0.8075*screenHeight);
	
	/************************************************************************/
	/* Private functions                                                    */
	/************************************************************************/
	bool Screen::initSDL(const string &screenCaption, unsigned int screenWidth, unsigned int screenHeight, unsigned int screenBpp) {
		if(SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO) == -1) {
			return false;
		}
		

		m_screenRoot = SDL_SetVideoMode( screenWidth, screenHeight, screenBpp, SDL_HWSURFACE); 

#ifdef _WII
		GXRModeObj *rmode = NULL;

		rmode = VIDEO_GetPreferredMode(NULL);

		if( CONF_GetAspectRatio() )
		{
			rmode->viWidth = 668;
			rmode->viXOrigin = (VI_MAX_WIDTH_PAL - 668)/2;
		}
#endif
		
		if(m_screenRoot == NULL) {
			return false;
			fprintf(stderr, "Unable to set video: %s\n", SDL_GetError());
			SDL_Delay( 5000 );
			exit(EXIT_FAILURE);
		}

		if( TTF_Init() == -1 ) { return false; } 
		
		//Sets the screen caption
		//SDL_WM_SetCaption(screenCaption.c_str(), NULL); 

		//m_pStatusFont = TTF_OpenFont( "lazy.ttf", 28 ); 
		
		return true;
	}

	Tile * Screen::findBaseEntity(const string &entName){
		Tile *pEnt = NULL;
		map<string, Tile *>::iterator iter = m_renderMap.find(entName);
		if (iter == m_renderMap.end()) return NULL;
		if (iter->first.size() > 0) {
			pEnt = iter->second;
		}
		return(pEnt);
	}

	/************************************************************************/
	/* Constructors				                                            */
	/************************************************************************/
	Screen::Screen(const string &screenCaption, unsigned int screenWidth, unsigned int screenHeight, unsigned int screenBpp) {
			m_isSDLInitialized = initSDL(screenCaption, screenWidth, screenHeight, screenBpp);
			m_lastTick = 0;
			fps.start();
			update.start();
			m_frame = 0;
	}
	Screen::~Screen() {
		SDL_Quit();
	}

	/************************************************************************/
	/* Functionalities                                                      */
	/************************************************************************/
	bool Screen::addImage(const Coord2D &position, const string &imageFile) {
		shared_ptr<SDLImage> pImage = TD_Resources::ResourceManager::loadImage(imageFile);
		m_renderVector.push_back(new Tile(position, pImage->getBounds(), imgTile, pImage));
		
		return true;
	}

	bool Screen::addImage(const Coord2D &position, const string &imageFile, const string &objName) {
		Tile *dObj;
		if((dObj = findBaseEntity(objName)) != NULL) {
			delete dObj;
		}
		if(objName.length() > 0) {
			shared_ptr<SDLImage> pImage = TD_Resources::ResourceManager::loadImage(imageFile);
			m_renderMap[objName] = new Tile(position, pImage->getBounds(), imgTile, pImage);
			return true;
		}
		return(false);
	}

	bool Screen::setStatusBarImage(const string &imageFile) {
		return(addImage(m_statusBarPos, imageFile, "statusBar"));
	}

	bool Screen::setRightMenuImage(const string &imageFile) {
		return(addImage(m_rightMenuPos, imageFile, "rightMenu"));
	}

	bool Screen::setCanvasImage(const string &imageFile) {
		return(addImage(m_drawingCanvasPos, imageFile, "drawingCanvas"));
	}

	bool Screen::setInfoBarImage(const string &imageFile) {
		return(addImage(m_infoBarPos, imageFile, "infoBar"));
	}

	bool Screen::setBackgroundImage(const string &imageFile) {
		return(addImage(Coord2D(0,0), imageFile, "backgroundImg"));
	}

	void Screen::addToRender(Tile *renderEnt) {
		
		m_renderVector.push_back(renderEnt);
	}

	/************************************************************************/
	/* Rendering functions                                                  */
	/************************************************************************/
	void Screen::render(unsigned int deltaTime) {
		if(m_isSDLInitialized) {
			BaseEntity *p_baseEnt;
			map<string, Tile *>::iterator iter = m_renderMap.begin();
			while(iter != m_renderMap.end()) {
				if (iter->first.size() > 0) {
					p_baseEnt = iter->second;
					p_baseEnt->render(m_screenRoot, deltaTime);
				}
				iter++;
			}
			
			for(unsigned int i = 0; i < m_renderVector.size(); i++) {
				m_renderVector[i]->render(m_screenRoot, deltaTime);
			}
			
		}
	}
}